import Pagination from '@/components/Pagination'
import ListFilter from '@/components/ListFilter'
import { parseTime } from '@/utils';
import { debounce } from 'lodash';
import { mapGetters } from 'vuex'

/**
 * @desc 基础列表混入参数
 * updateSort - 更新排序编号，绑定表内input@change
 */
export default {
    components: { Pagination, ListFilter },
    data() {
        return {
            disableDels: true,
            listQuery: {
                page: 1,
                limit: 10
            },
            list: [],
            temp: {},
            selectItems: [],
            total: 0,
            listLoading: true
        }
    },
    computed: {
        ...mapGetters([
            'language'
        ])
    },
    methods: {
        parseTime,
        getList(queryArg) {
            if (queryArg) {
                if (queryArg.reset) {
                    this.extraQuery = {}
                }
                this.listQuery = { page: queryArg.page || 1, limit: queryArg.limit || 10 }
            }
            this.listQuery = { ...this.listQuery, ...this.fixedQuery, ...this.extraQuery };
            this.listLoading = true;
            this.listSearch(this.listQuery).then(response => {
                this.list = response.data;
                this.total = response.count;
                this.listLoading = false;
            });
        },
        resetTemp() {
            this.temp = {};
        },
        // 多选
        tableSelect(selection) {
            this.slideFormVisible = false;
            this.disableDels = selection.length <= 0;
            this.selectItems = selection.map((i) => {
                return i[this.itemId];
            });
        },
        // 多选删除
        handleDeletes() {
            console.log(this.selectItems);
            this.$confirm(this.$t('extra.confirmDelete'), this.$t('global.hint'), {
                customClass: 'custom-msgbox',
                center: true
            }).then(() => {
                this.listDelete(this.selectItems).then(res => {
                    this.getList(); // 刷新列表
                    this.$message({
                        type: 'success',
                        message: this.$t('tips.successDel')
                    });
                });
            });
        },
        // 输入排序编号
        updateSort: debounce(function(row, val) {
            val = Number(val)
            row[this.sortName] = val || ''
            const param = { [this.itemId]: row[this.itemId] }
            param[this.sortName] = val || 0
            this.listUpdate(param).catch(res => {
                this.$notify({
                    type: 'error',
                    title: this.$t('global.fail'),
                    message: this.$t('tips.reqExcp')
                })
                row[this.sortName] = ''
            })
        }, 300),
        // 编辑
        handleEdit(row) {
            this.$router.push({ path: `${this.$route.fullPath.replace('/list', '')}/update/${row[this.itemId]}` });
        },
        // 新增
        handleCreate() {
            this.$router.push({ path: `${this.$route.fullPath.replace('/list', '')}/insert`, query: { lang: this.languageVersion }});
        }
    }
}
